package com.framework.common.domain;

import com.framework.common.Enums.FrameworkErrorCodeEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/4/12 14:52
 * @since JDK 1.8
 */
public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = -8157613083634272196L;

    public R() {
        put("code", FrameworkErrorCodeEnum.SUCCESS.getCode());
        put("msg", FrameworkErrorCodeEnum.SUCCESS.getMsg());
        put("success", true);
    }

    public static R error() {
        return error(FrameworkErrorCodeEnum.UNKOWN_ERR);
    }

    public static R error(String msg) {
        return error("500", msg);
    }

    public static R error(FrameworkErrorCodeEnum error) {
        return error(error.getCode(), error.getMsg());
    }

    public static R error(String code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.put("success", false);
        return r;
    }

    public static R ok(String code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.put("success", true);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        r.put("success", true);
        return r;
    }

    public static R data(Object obj) {
        R r = new R();
        r.put("data", obj);
        r.put("success", true);
        return r;
    }

    public static R okData(Object obj) {
        R r = new R();
        r.put("data", obj);
        r.put("code", FrameworkErrorCodeEnum.SUCCESS.getCode());
        r.put("msg", FrameworkErrorCodeEnum.SUCCESS.getMsg());
        r.put("success", true);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        r.put("success", true);
        return r;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
